﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TPutawayOrderRepository : BaseRepository<TPutawayOrder>, ITPutawayOrderRepository
    {
        private readonly IBarCodeRuleRepository _BarCodeRuleRepositoy;
        public TPutawayOrderRepository(DFGK_DB context, IMapper mapper, IBarCodeRuleRepository barCodeRuleRepositoy) : base(context, mapper)
        {
            _BarCodeRuleRepositoy = barCodeRuleRepositoy;
        }

        /// <summary>
        /// 查询任务管理信息
        /// </summary>
        /// <param name="code">任务单号</param>
        /// <returns></returns>
        public TPutawayOrder Getputawayorder(Expression<Func<TPutawayOrder, bool>> where)
        {
            var list = Context.TPutawayOrders.Where(where).FirstOrDefault();
            return list;

        }
        /// <summary>
        /// 修改任务状态
        /// </summary>
        /// <returns></returns>
        public bool UpdatePutawayorder(TPutawayOrder order)
        {
            var con = Context.TPutawayOrders.Update(order);
            return Context.SaveChanges() > 0;
        }

        public bool AddTLogs()
        {
            TLog tlog = new TLog();
            tlog.LogType = 1;
            tlog.Status = 1;
            tlog.CreateDate = DateTime.Now;
            tlog.ModuleName = "堆垛机测试";
            Context.TLogs.Add(tlog);
            return Context.SaveChanges() > 0;
        }


        public WCSRequestReturnDto WCSRequestReturn(WCSRequestDto wcsRequestDto)
        {
            TLog log = new TLog();
            log.ActionType = 1;
            log.Status = 1;
            log.CreateDate = DateTime.Now;
            log.Description = "扫码枪扫描入库成功，托盘号:" + wcsRequestDto.palletCode;
            Context.TLogs.Add(log);
            Context.SaveChanges();

            WCSRequestReturnDto wCSRequestReturnDto = new WCSRequestReturnDto();

            var putawayOrderlist = Context.TPutawayOrders.ToList();
            if (putawayOrderlist.Count() <= 0)
            {
                return null;
            }

            var putawayOrderDetails = Context.TPutawayOrderDetails.Where(x => x.PalletBarcode == wcsRequestDto.palletCode).OrderByDescending(x => x.Id);
            if (putawayOrderDetails.Count() <= 0)
            {
                return null;
            }
            var putOrderList = Context.TPutawayOrders.FirstOrDefault(x => x.Id == putawayOrderDetails.FirstOrDefault().PutawayOrderId);
            wCSRequestReturnDto.palletCode = wcsRequestDto.palletCode;
            wCSRequestReturnDto.LocationCode = putOrderList.LocationCode;
            wCSRequestReturnDto.taskId = putOrderList.Code;
            return wCSRequestReturnDto;

        }

        public bool UpdEquipment(out string message, List<TEquipment> entity)
        {
            try
            {
                TLog log = new TLog();
                log.LogType = 1;
                log.Status = 1;
                log.Description = "设备状态测试";
                log.ActionType = 6;
                log.UserName = "admin";
                log.CreateDate = DateTime.Now;
                Context.TLogs.Add(log);
                if (Context.SaveChanges() <= 0)
                {
                    message = "失败";
                    return false;
                }
                message = "";
                var equipmentList = Context.TEquipment.AsQueryable();
                foreach (var item in entity)
                {
                    if (item.EquipmentCode == "" || item.EquipmentCode == null)
                    {
                        message = "请求编码为空";
                        return false;
                    }
                    var OtherList = equipmentList.Where(x => x.EquipmentCode.Equals(item.EquipmentCode));
                    if (OtherList.Count() <= 0)
                    {
                        TEquipment other = new TEquipment();
                        other.EquipmentCode = item.EquipmentCode;
                        other.EquipmentName = item.EquipmentName;
                        other.Status = item.Status;
                        Context.TEquipment.Add(other);
                        if (Context.SaveChanges() <= 0)
                        {
                            message = "添加设备表数据失败";
                            return false;
                        }
                    }
                    else
                    {
                        var list = Context.TEquipment.FirstOrDefault(x => x.EquipmentCode == item.EquipmentCode);
                        list.EquipmentCode = item.EquipmentCode;
                        list.EquipmentName = item.EquipmentName;
                        list.Status = item.Status;
                        Context.TEquipment.Update(list);
                        if (Context.SaveChanges() <= 0)
                        {
                            message = "更新设备表数据失败";
                            return false;
                        }
                    }

                }
                return true;
            }
            catch
            {
                message = "更新失败";
                return false;
            }

        }


        public TStackingStorage GetStackingStorage(out string message, string palletBarCode)
        {
            message = "";
            var stackingStorageList = Context.TStackingStorages.Where(x => x.PalletBarcode == palletBarCode && x.Status == 1);
            if (stackingStorageList.Count() <= 0)
            {
                message = "未查询到托盘" + palletBarCode + "的相关入垛信息";
                return null;
            }
            return stackingStorageList.First();
        }


        public List<TShippingLockCell> GetShippingLockCells(out string message, string palletBarCode)
        {
            message = "";
            var shippingLockList = Context.TShippingLockCells.Where(x => x.PalletBarcode == palletBarCode && x.IsPicking == false).ToList();
            if (shippingLockList.Count() <= 0)
            {
                message = "暂无此出库详情信息";
                return null;
            }
            List<TShippingLockCell> list = new List<TShippingLockCell>();
            foreach (var item in shippingLockList)
            {
                var shippingOrderDetailList = Context.TShippingOrderDetails.Where(x => x.Id == item.OrderDetailId).ToList();
                if (shippingOrderDetailList.Count() <= 0)
                {
                    message = "出库单信息为空";
                    return null;
                }
                list.Add(item);
            }
            return list;
        }
    }
}
